package edu.rmit.client;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import edu.rmit.contant.RoleType;
import edu.rmit.domain.User;
import edu.rmit.service.IUserService;
import edu.rmit.util.PromotDialog;

/**
 * This class is to display the user name and password, and user can use these
 * to implement the login function.
 * 
 * @author Thaw Zin
 * @version 1.0
 * 
 */
@SuppressWarnings("serial")
public class LoginPanel extends JPanel {

	/**
	 * The frame object to initialize the window.
	 */
	private JFrame frame;

	/**
	 * The window initialize height.
	 */
	private static int HEIGHT = 200;

	/**
	 * The window initialize height.
	 */
	private static int WIDTH = 400;

	/**
	 * The label which display the user name.
	 */
	private JLabel usernameLabel;

	/**
	 * The label which display the password.
	 */
	private JLabel passwordLabel;

	/**
	 * The text field which display the username.
	 */
	private JTextField usernameField;

	/**
	 * The password which display the password.
	 */
	private JPasswordField passwordField;

	/**
	 * The button which when a user clicked, will cause the user to login.
	 */
	private JButton confirmButton;

	/**
	 * The button which when a user clicked, will cause the user to cancel
	 * login.
	 */
	private JButton cancelButton;

	/**
	 * The user related service.
	 */
	private IUserService userService;

	/**
	 * The client instance which can get the user related service.
	 */
	private Client client;

	/**
	 * The constructor to initialize the login panel and some corresponding
	 * control.
	 * 
	 * @param client
	 *            The client instance which can get the services.
	 */
	public LoginPanel(Client client) {

		this.client = client;

		frame = new JFrame("User Login");
		userService = client.getUserService();

		JPanel contentPanel = new JPanel();
		contentPanel.setLayout(new GridLayout(3, 1));

		usernameLabel = new JLabel("username:");
		passwordLabel = new JLabel("password:");

		usernameField = new JTextField(10);
		passwordField = new JPasswordField(10);

		confirmButton = new JButton("Confirm");
		cancelButton = new JButton("Cancel");

		confirmButton.addActionListener(new LoginActionListener());

		cancelButton.addActionListener(new ListenerCancelLogin());

		JPanel usernamePanel = new JPanel();

		usernamePanel.add(usernameLabel);
		usernamePanel.add(usernameField);
		contentPanel.add(usernamePanel);

		JPanel passwordPanel = new JPanel();

		passwordPanel.add(passwordLabel);
		passwordPanel.add(passwordField);
		contentPanel.add(passwordPanel);

		JPanel commandPanel = new JPanel();
		commandPanel.add(confirmButton);
		commandPanel.add(cancelButton);
		contentPanel.add(commandPanel);

		frame.setContentPane(contentPanel);
		initFrame();
	}

	/**
	 * This method is to implement the initialize the window, and some
	 * corresponding control.
	 */
	private void initFrame() {
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setVisible(true);
		frame.setSize(WIDTH, HEIGHT);

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = frame.getSize();
		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height;
		}
		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width;
		}
		frame.setLocation((screenSize.width - frameSize.width) / 2,
				(screenSize.height - frameSize.height) / 2);
		
	}

	/**
	 * This class is to implement the logic when the user click the login
	 * button.
	 * 
	 * @author Thaw Zin
	 * @version 1.0
	 * 
	 */
	class LoginActionListener implements ActionListener {
		/**
		 * Implement the logic when a user clicked the login button. And cause
		 * the user to implement the login function.
		 */
		@Override
		public void actionPerformed(ActionEvent event) {
			String userName = usernameField.getText().trim();
			String password = new String(passwordField.getPassword());
			try {
				User user = userService.getUser(userName);
				String title = "Login Failed";
				String reason = "";
				if (user == null) {
					reason = "Username or password is wrong!";
					PromotDialog.errorMessageBox(null, title, reason);
				} else if (user.getPassWord().equals(password)) {
					if (user.getRoleType() == RoleType.USER && !user.IsActive()) {
						reason = "You have been suspended by admin, please contact admin!";
						PromotDialog.errorMessageBox(null, title, reason);
						return;
					}
					if (user.isIslogin() == true) {
						reason = "Your username '" + userName
								+ "' has been logined in aother place!";
						PromotDialog.errorMessageBox(null, title, reason);
						return;
					} else {
						userService.changeLoginStatus(userName, true);
						frame.dispose();
						new GroupListPanel(client, user);
					}
				}

			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * This class is to implement the logic when the user click the cancel
	 * button.
	 * 
	 * @author Thaw Zin
	 * @version 1.0
	 * 
	 */
	class ListenerCancelLogin implements ActionListener {

		/**
		 * Changes the text of the label.
		 * 
		 * @param event
		 *            the event object.
		 */
		public void actionPerformed(ActionEvent event) {
			frame.dispose();
			new WelcomePanel(client);
		}
	}
}
